import axios from 'axios';
import router from '../router'
import Vant, { Dialog, Lazyload, Toast } from 'vant';
import { aesDecrypt, aesEncrypt } from './encrypter';
import store from '../store'
import i18n from "../plugin/lang/i18n";

export default {
  install: Vue => {
    Vue.use(i18n);
    Vue.use(Vant);
    Vue.use(Lazyload, {
      lazyComponent: true,
    });
    Vue.prototype.$axios = axios;
    axios.defaults.timeout = 10000;

    if (process.env.NODE_ENV === 'development')
      setPath(process.env.VUE_APP_REQUEST_URL);
    else if (process.env.NODE_ENV === 'production')
      setPath(process.env.VUE_APP_REQUEST_URL);

    function setPath(path) {
      axios.defaults.baseURL = path;
    }

    function errorLog(message, tips = true) {
      if (tips) {
        console.error(message);
      }
    }

    Vue.prototype.$encPost = param => {
      if (process.env.VUE_APP_LOG === 'true') {
        console.log(param);
      }
      store.state.showLoading = true;

      return new Promise((resolve, reject) => {
        let token;
        if (process.env.VUE_APP_TOKEN_VUEX === 'true') {
          token = store.state.token;
        } else {
          token = localStorage.getItem("token") || "";
        }

        if (token && !token.startsWith("Bearer ")) {
          token = "Bearer " + token;
        }

        const config = {
          headers: {
            'Authorization': token
          }
        };

        let $axios;

        if (param.upload) {
          if (param.data instanceof File) {
            const formData = new FormData();
            formData.append("data", param.data);
            param.data = formData;
          }
          $axios = axios.post("/upload", param.data, config);
        } else {
          const data = {
            source: 'web',
            version: 'v1',
            module: param.module,
            interface: param.interface,
            timestamp: Math.round(new Date().getTime() / 1000),
          };
          $axios = axios.post(param.url || "/portal", aesEncrypt(data, param.data, param.enc), config);
        }

        if ($axios) {
          $axios.then((response) => {
            store.state.showLoading = false;
            let dataAxios = aesDecrypt(response.data, param.enc);
            if (process.env.VUE_APP_LOG === 'true') {
              console.log(dataAxios);
            }
            const { code } = dataAxios;
            if (!code) {
              resolve(dataAxios.data || dataAxios);
            } else {
              switch (code) {
                case 4700:
                  Toast(i18n.t('m.axios.登录信息失效'));
                  localStorage.setItem("token", '');
                  errorLog(dataAxios.message, param.tips);
                  reject(dataAxios);
                  router.push("/login");
                  if (process.env.VUE_APP_OPEN_WEB3 === 'true' || process.env.VUE_APP_OPEN_WEB3_TRON === 'true'){
                    setTimeout(()=>{
                      window.location.reload();
                    },800)
                  }
                  break;
                case 4800:
                case 4810:
                  errorLog(dataAxios.message, param.tips);
                  Toast(dataAxios.message);
                  reject(dataAxios);
                  break;
                case 4500:
                  Dialog.alert({ message: dataAxios.message });
                  errorLog(dataAxios.message, param.tips);
                  reject(dataAxios);
                  break;
                case 4890:
                  Toast(dataAxios.message, param.tips);
                  reject(dataAxios);
                  break;
                default:
                  errorLog('网络异常，请稍后..', param.tips);
                  Toast(i18n.t('m.axios.网络异常'));
                  reject(dataAxios);
                  break;
              }
            }
          }).catch(err => {
            store.state.showLoading = false;
            if (!err.response) {
              console.error("error -> 网络请求失败，请检查请求地址或请求参数信息");
              reject(err);
            } else if (err.response.status !== 200) {
              let message;
              switch (err.response.status) {
                case 400:
                  message = i18n.t('m.axios.请求错误');
                  break;
                case 401:
                  message = i18n.t('m.axios.未授权');
                  break;
                case 403:
                  message = i18n.t('m.axios.拒绝访问');
                  break;
                case 404:
                  message = `${i18n.t('m.axios.请求地址出错')}:${err.response.config.url}`;
                  break;
                case 408:
                  message = i18n.t('m.axios.请求超时');
                  break;
                case 500:
                  message = i18n.t('m.axios.服务器内部错误');
                  break;
                case 501:
                  message = i18n.t('m.axios.服务未实现');
                  break;
                case 502:
                  message = i18n.t('m.axios.网关错误');
                  break;
                case 503:
                  message = i18n.t('m.axios.服务不可用');
                  break;
                case 504:
                  message = i18n.t('m.axios.网关超时');
                  break;
                case 505:
                  message = i18n.t('m.axios.HTTP版本不受支持');
                  break;
                case "ECONNABORTED":
                  message = i18n.t('m.axios.请求超时');
                  break;
                default:
                  message = i18n.t('m.axios.意外错误');
                  break;
              }
              console.error("error ->", message);
              err.message = message;
              reject(err);
            } else {
              reject(err);
            }
          });
        }
      });
    }
  }
}
